package cn.com.digitalnet.ad.web;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.digitalnet.ad.base.CrudController;
import cn.com.digitalnet.ad.entity.Role;
import cn.com.digitalnet.ad.entity.User;
import cn.com.digitalnet.ad.service.RoleService;
import cn.com.digitalnet.ad.service.UserService;

@RequiresRoles("Admin")
@Controller
@RequestMapping(value = "/user")
public class UserController extends CrudController<User>{

	private static final String PAGE_SIZE = "10";
	
	{
		sortTypes.put("id", "自动");
		sortTypes.put("loginName", "登录名");
		sortTypes.put("name", "用户名");
		sortTypes.put("isenabled", "状态");
	}
	
	@Autowired private UserService service;
	@Autowired private RoleService roleService;

	@Override
	protected UserService service() {
		return service;
	}

	@Override
	protected String english() {
		return "user";
	}

	@Override
	protected String chinese() {
		return "用户";
	}

	@Override
	protected String parentMenu() {
		return "";
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "id") String sortType,
			@RequestParam(value = "sortDire", defaultValue = "id") String sortDire,
			Model model,
			ServletRequest request) {
		return standardList(pageNumber, pageSize, sortType, sortDire, model, request);
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		//角色们
		List<Role> roles = roleService.findEnabledAll();
		model.addAttribute("roles", roles);
		return standardCreateForm(model);
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid User user,
			@RequestParam(value = "roleList") List<Long> checkedRoleList, 
			@RequestParam(value = "accounts", required=false) String accounts, 
			@RequestParam(value = "brands", required=false) String brands, 
			@RequestParam(value = "mediaList", required=false) List<Long> checkedMediaList, 
			RedirectAttributes redirectAttributes) {
		for (Long roleId : checkedRoleList) {
			Role r = new Role(roleId);
			user.getRoleList().add(r);
		}
		
		return standardCreate(user, redirectAttributes);
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		//角色们
		List<Role> roles = roleService.findEnabledAll();
		model.addAttribute("roles", roles);
		
		
		return this.standardUpdateForm(id, model);
	}

	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("_entity_") User user,
			@RequestParam(value = "roleList") List<Long> checkedRoleList, 
			RedirectAttributes redirectAttributes) {
		for (Long roleId : checkedRoleList) {
			Role r = new Role(roleId);
			user.getRoleList().add(r);
		}
		
		return standardUpdate(user, redirectAttributes);
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		return standardDelete(id, redirectAttributes);
	}

	@ModelAttribute
	public void getEntity(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		standardGetEntity(id, model);
	}

	@RequestMapping(value = "publish/{id}", method = RequestMethod.GET)
	public String publish(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		return standardSubmit(id, redirectAttributes);
	}

	@RequestMapping(value = "checkLoginName")
	@ResponseBody
	public String checkLoginName(@RequestParam("oldLoginName") String oldLoginName,
			@RequestParam("loginName") String loginName) {
		if (loginName.equals(oldLoginName)) {
			return "true";
		} else if (service.findUserByLoginName(loginName) == null) {
			return "true";
		}

		return "false";
	}
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,
	                              ServletRequestDataBinder binder) throws Exception {
	    binder.setDisallowedFields("roleList","accountList","brandList","mediaList");
		//对于需要转换为Date类型的属性，使用DateEditor进行处理
	    //binder.registerCustomEditor(Date.class, new DateEditor());
	}

}
